import java.awt.Point;


public class Stage {
	
	private final int APPLET_WIDTH = 600;
	private final int APPLET_HEIGHT = 352;
	private int[] groundLevels;
	private int[] platformLevelOne;
	private int[] platformLevelTwo;
	
	public Stage(){
		groundLevels     = new int[APPLET_WIDTH+1];
		platformLevelOne = new int[APPLET_WIDTH+1];
		platformLevelTwo = new int[APPLET_WIDTH+1];
		for(int i = 0; i <= APPLET_WIDTH; i++){
			groundLevels[i] = APPLET_HEIGHT + 100;
		}		
	}
	
	//Sets all stage levels to be at the bottom of the applet
	public void reset(){
		for (int i = 0; i <= 600;i++){
			groundLevels[i]     = APPLET_HEIGHT;
			platformLevelOne[i] = APPLET_HEIGHT;
			platformLevelTwo[i] = APPLET_HEIGHT;
		}
	}
	
	//Sets levels for the ground from the x-values
	//start to end
	public void setGroundLevels(int start, int end, int height){
		for(int i = start; i <= end; i++){
			groundLevels[i] = height;
		}
	}
	
	//Sets first platforms from start to end
	public void setPlatformOneLevels(int start, int end, int height){
		for(int i = start; i <= end; i++){
			platformLevelOne[i] = height;
		}
	}
	
	//Sets second platforms from start to end
	public void setPlatformTwoLevels(int start, int end, int height){
		for(int i = start; i <= end; i++){
			platformLevelTwo[i] = height;
		}
	}

	//Find the ground level for a point
	public int getGround(Point p){
		if (p.x < 0){
			p.x++;
		}
		int groundDiff = (int)(groundLevels[(int)p.getX()] - p.getY());
		if(groundDiff < 0){
			groundDiff = 1000;
		}
		int platOneDiff = (int) (platformLevelOne[(int) p.getX()] - p.getY());
		if(platOneDiff < 0){
			platOneDiff = 1000;
		}
		int platTwoDiff = (int) (platformLevelTwo[(int) p.getX()]-p.getY());
		if(platTwoDiff < 0){
			platTwoDiff = 1000;
		}
		if(groundDiff < platOneDiff || groundDiff < platOneDiff){
			return groundLevels[(int)p.getX()];
		}
		else if(platOneDiff < platTwoDiff){
			return platformLevelOne[(int)p.getX()];		
		}
		else{
			return platformLevelTwo[(int)p.getX()];
		}
	}
	
	public int getRelativeGround(Point p){
		int ground = getGround(p);
		int base;
		if(p.getY()<platformLevelTwo[(int)p.getX()] - 60){
			base=ground;
		}
		else if(p.getY() <= platformLevelTwo[(int) p.getX()]){
			base = platformLevelOne[(int) p.getX()];
		}else{
			base = groundLevels[(int) p.getX()];
		}
		return Math.abs(base - ground);
	}
	
	//returns negative if a fall is needed; positive if a jump is needed; 0 if flat for a single level
	//public int canMove(int current, int next){
}
